﻿@{
    ViewBag.Title = "";
    Layout = "~/Views/Shared/_Index.cshtml";
}
@using Raffles.OA.Data
@using Raffles.OA.Utility
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-info">
                <div class="box-body">
                    <div class="table-responsive">
                        <div class="btn-group-sm" id="toolbar" role="group">
                            <a id="btnAdd" authorize="yes" class="btn btn-success" onclick="showForm('')"><i class="fa fa-plus"></i> 新增</a>
                            <a id="btnLog" authorize="yes" class="btn btn-info" onclick="showAutoJobLogForm()"><i class="fa fa-list"></i> 执行日志</a>
                            <div class="col-sm-3 pull-right">
                                <div class="input-group input-group-sm">
                                    <input type="text" id="keyWord" col="KeyWord" class="form-control" placeholder="请输入关键字查询" />
                                    <div class="input-group-btn">
                                        <button type="button" class="btn btn-primary" onclick="searchGrid()"><i class="fa fa-search"></i></button>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <table id="gridTable" class="table table-hover table-striped text-nowrap">
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<script type="text/javascript">
    $(function () {
        initGrid();
    });

    function initGrid() {
        $('#gridTable').frameTable({
            url: '@Url.Action("GetPageListJson", "AutoJob")',
            sortName: 'CreateOn',
            sortOrder: 'Desc',
            columns: [
                { field: 'Id', title: 'Id', visible: false },
                { field: 'JobGroupName', title: '任务组', halign: "center", align: "center" },
                { field: 'JobName', title: '任务名称', halign: "center", align: "center" },
                { field: 'DllName', title: '程序集', halign: "center", align: "center" },
                {
                    field: 'ClassName', title: '执行方法', halign: "center", align: "center",
                    formatter: function (value, row, index) {
                        return row.ClassName + '.' + row.FunctionName;
                    }
                },
                { field: 'CronExpression', title: 'Cron表达式', halign: "center", align: "center" },
                { field: 'Remark', title: '任务说明', halign: "center", align: "center" },
                {
                    field: 'JobStatus', title: '运行状态', halign: "center", align: "center",
                    formatter: function (value, row, index) {
                        if (row.JobStatus == '@((int)JobStatusEnum.Run)') {
                            return '<span class="label label-success">@JobStatusEnum.Run.GetDescription()</span>';
                        } else {
                            return '<span class="label label-danger">@JobStatusEnum.Pause.GetDescription()</span>';
                        }
                    }
                },

                { field: 'StartTime', title: '开始时间', halign: "center", align: "center" },
                { field: 'EndTime', title: '结束时间', halign: "center", align: "center" },
                { field: 'NextStartTime', title: '下次运行时间', halign: "center", align: "center" },

                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.JobStatus == '@((int)JobStatusEnum.Run)') {
                            actions.push('<a href="javascript:void(0);" authorize="yes" id="btnPause" class="btn btn-warning btn-xs" onclick="ChangeJobStatus(\'' + row.Id + '\',@((int)JobStatusEnum.Pause))"><i class="fa fa-pause-circle-o"></i> 暂停</a> ');
                        } else {
                            actions.push('<a href="javascript:void(0);" authorize="yes" id="btnEdit" class="btn btn-primary btn-xs" onclick="showForm(\'' + row.Id + '\')"><i class="fa fa-edit"></i> 编辑</a> ');
                            actions.push('<a href="javascript:void(0);" authorize="yes" id="btnDelete" class="btn btn-danger btn-xs" onclick="deleteData(\'' + row.Id + '\')"><i class="fa fa-trash-o"></i> 删除</a> ');
                            actions.push('<a href="javascript:void(0);" authorize="yes" id="btnRun" class="btn btn-success btn-xs" onclick="ChangeJobStatus(\'' + row.Id + '\',@((int)JobStatusEnum.Run))"><i class="fa fa-play-circle-o"></i> 运行</a> ');
                        }

                        return actions.join('');
                    }
                }
            ],
            queryParams: function (params) {
                var pagination = $('#gridTable').frameTable('getPagination', params);
                var queryString = $('#toolbar').getWebControls(pagination);
                return queryString;
            }
        });
    }

    function searchGrid() {
        $('#gridTable').frameTable('search');
    }

    function showForm(id) {
        frame.openDialog({
            title: !!id ? '编辑任务调度': '添加任务调度',
            content: '@Url.Content("/Manage/AutoJob/Form")' + '?id=' + id,
            width: '768px',
            height: '530px',
            callback: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                iframeWin.saveForm(index);
            }
        });
    }

    function deleteData(id) {
        frame.confirm("确定要删除该数据吗？", function () {
            frame.ajax({
                url: '@Url.Content("/Manage/AutoJob/DeleteData")' + '?id=' + id,
                type: 'post',
                success: function (obj) {
                    if (obj.success) {
                        frame.msgSuccess(obj.message);
                        searchGrid();
                    }
                    else {
                        frame.msgError(obj.message);
                    }
                }
            });
        });
    }

    //运行、暂停job
    function ChangeJobStatus(id, jobStatus) {
        var postData = {};
        postData.Id = id;
        postData.JobStatus = jobStatus;
        var result = "";
        if (jobStatus == '@((int)JobStatusEnum.Run)') {
            result = "运行";
        }
        else {
            result = "暂停";
        }
        frame.confirm("确认要[<span class='text-red text-bold'>" + result + "</span>]该任务吗？", function () {
            frame.ajax({
                url: '@Url.Content("/Manage/AutoJob/ChangeJobStatus")',
                type: "post",
                data: postData,
                success: function (obj) {
                    if (obj.success) {
                        frame.msgSuccess(obj.message);
                        searchGrid();
                    }
                    else {
                        frame.msgError(obj.message);
                    }
                }
            });
        });
    }

    //执行日志
    function showAutoJobLogForm() {
        var url = '@Url.Content("/Manage/AutoJobLog/Index")'
        createMenuItem(url, "调度日志");
    }
</script>